package ah.html;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jjkk.beans.Newjibing;
import com.jjkk.beans.Newkeshi;
import com.jjkk.beans.Newkjbiao;

import sessionFactory.HibernateSessionFactory;


public class ImportData {

	/**
	 * @author dingdahai
	 * @date Nov 19, 2009 11:17:01 AM
	 * @param args
	 */
	public static void main(String[] args) {
		//new ImportData().insertData();
	}
	
	public void insertData() {
		//部位
		Map<String,String> partMap = new HashMap<String,String>();
		partMap.put("鼻", "头部");
		partMap.put("耳", "头部");
		partMap.put("口", "头部");
		partMap.put("牙", "头部");
		partMap.put("头", "头部");
		partMap.put("眼", "头部");
		partMap.put("眉", "头部");
		partMap.put("上肢", "四肢");
		partMap.put("下肢", "四肢");
		partMap.put("颈部", "");
		partMap.put("胸部", "");
		partMap.put("背部", "");
		partMap.put("腹部", "");
		partMap.put("腰部", "");
		partMap.put("盆腔", "");
		partMap.put("臀部", "");
		partMap.put("男性股沟", "生殖部位");
		partMap.put("女性盆骨", "生殖部位");
		partMap.put("皮肤", "");
		partMap.put("全身", "");
		partMap.put("毛发", "");
		partMap.put("其他", "");
		partMap.put("", "");
		
		
		//保存疾病和科室
		List<String> jibingList = new ArrayList<String>();
		jibingList.add("");//插入空,疾病名字不能为空
		Map<String,Newkeshi> keshiMap = new HashMap<String,Newkeshi>();
		
		
		//把未插入的数据写入文件
		File unsavedFile = new File("D:/39jk2/unsavedFile.txt");
		if (!unsavedFile.exists()) {
			try {
				unsavedFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return;
			}
		}
		
		//打开写入流
		BufferedWriter bw = null;
		/*try {
			bw = new BufferedWriter(new FileWriter(unsavedFile));
		} catch (IOException e) {
			e.printStackTrace();
			return ;
		}*/
		
		//open session
		Session session = HibernateSessionFactory.getSession();
		
		//初始化科室map
		Query query = session.createQuery("from Newkeshi");
		List officename222 = query.list();
		Iterator it222 = officename222.iterator();
		while (it222.hasNext()) {
			Newkeshi newkeshi = (Newkeshi)it222.next();
			keshiMap.put(newkeshi.getOfficename(), newkeshi);
		}
		
		Transaction tx = null;
		tx = session.beginTransaction();
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("D:/39jk2/result.txt"));
			String jb = null;
			jb = br.readLine();
			
			while (jb!=null && !jb.equals("")) { //文件最后一行为空行 结果为null
				
				
				String[] temps = jb.split("\\|");
				
				//从疾病表中查找,是否存在一样的疾病名字
				String selectStr = null;
				//selectStr="from Newjibing jb where jb.disname='"+temps[0].trim()+"'";
				//query = session.createQuery(selectStr);
				//List list = query.list();
				if (jibingList.contains(temps[0].trim())) {
				//if (list!=null && list.size()>0 && list.get(0)!=null) {
					//bw.write("record exist: ");
					//bw.write(jb);
					//bw.write("\n");
				} else {//insert into database
					tx.begin();
					//保存疾病
					Newjibing newjibing = new Newjibing();
					newjibing.setDisname(temps[0]);//名字
					newjibing.setPinyinma(temps[4]);//拼音码
					newjibing.setBak1(temps[1]);//别名
					
					session.saveOrUpdate(newjibing);
					jibingList.add(newjibing.getDisname());//添加疾病名字进list
					
					String[] partArray = new String[]{""};
					if (!"".equals(temps[3])) {//部位
						partArray = temps[3].split("@");
					}
					
					/*
					 * 取科室
					 * 科室如果没有则保存进数据库中
					 * 循环每个科室,循环每个疾病
					 */
					String[] officeArray = new String[]{""};
					if (!"".equals(temps[2])) {
						officeArray = temps[2].split("@");
					}
					
					for (int i=0; i<officeArray.length; i++) {
						Newkeshi newkeshi = null;
						if (!officeArray[i].equals("")) {//有名称则不为空,有科室id
							//selectStr="from Newkeshi ks where ks.officename=:officename";
							//Query query2 = session.createQuery(selectStr);
							//query2.setParameter("officename", officeArray[i]);
							//newkeshi = (Newkeshi)query2.uniqueResult();//TODO
							
							if (keshiMap.get(officeArray[i])==null) {
								newkeshi = new Newkeshi();
								newkeshi.setOfficename(officeArray[i]);
								newkeshi.setParentId("0");
								
								session.saveOrUpdate(newkeshi);
								keshiMap.put(newkeshi.getOfficename(), newkeshi);
							} else {
								newkeshi = keshiMap.get(officeArray[i]);
							}
						}

						//遍历部位,保存中间表
						for (int j=0; j<partArray.length; j++) {
							Newkjbiao newkjbiao = new Newkjbiao();
							
							String tempPart = (String)partMap.get(partArray[j]);
							if (tempPart==null || tempPart.equals("")) {
								newkjbiao.setBak1(partArray[j]);//部位1
								newkjbiao.setBak2("");//部位2
							} else {
								newkjbiao.setBak1(tempPart);//部位1
								newkjbiao.setBak2(partArray[j]);//部位2
							}
							
							newkjbiao.setNewjibing(newjibing);
							newkjbiao.setNewkeshi(newkeshi);
							
							session.saveOrUpdate(newkjbiao);
						}
					}
					tx.commit();
				}
				
				jb = br.readLine();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br!=null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				br = null;
			}
			
			session.close();
		}
		
	}
	
	
	//test
	public void viewData() {
		
		Set allOffice = new HashSet();
		Set allPart = new HashSet();
		Map jibinNameMap = new HashMap();
		
		BufferedReader br = null;
		try {
			
			br = new BufferedReader(new FileReader("D:/39jk2/result.txt"));
			String jb = null;
			jb = br.readLine();
			while (jb!=null && !jb.equals("")) { //文件最后一行为空行 结果为null 
				String[] temps = jb.split("\\|");
				
				Object obj = jibinNameMap.get(temps[0]);
				if (obj!=null) {
					System.out.println(jb);
				}
				jibinNameMap.put(temps[0], jb);
				
				if (!"".equals(temps[2])) {
					if (temps[2].contains("@")) {
						String[] tempOffices = temps[2].split("@");
						for (int i=0; i<tempOffices.length; i++) {
							allOffice.add(tempOffices[i]);
						}
					} else {
						allOffice.add(temps[2]);
					}
				}
				
				if (!"".equals(temps[3])) {
					if (temps[3].contains("@")) {
						String[] tempParts = temps[2].split("@");
						for (int i=0; i<tempParts.length; i++) {
							allPart.add(tempParts[i]);
						}
					} else {
						allPart.add(temps[3]);
					}
				}
				
				jb = br.readLine();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br!=null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				br = null;
			}
		}
		
		Iterator it = allPart.iterator();
		while (it.hasNext()) {
			//System.out.println(it.next());
		}
		
		
		it = allOffice.iterator();
		Session session = HibernateSessionFactory.getSession();
		String hql = "select nks.officename from Newkeshi nks where nks.officename=:officename";
		System.out.println("************");
		while (it.hasNext()) {
			Query query = session.createQuery(hql);
			Object obj = it.next();
			query.setParameter("officename", obj);
			if (query.list()!=null && query.list().size()>0) {
				//System.out.println(query.list().get(0));
				it.remove();
			} else {
				//System.out.println(obj);
			}
		}
		session.close();
	}

}
